
import { defineStore } from "pinia";
import type { RecordLocation } from '@/types/book'
export const useReadRecordStore = defineStore('readRecord', {
    state: () => {
        return {
            recentBooks: [] as string[],
            recordLocation: {} as { [key: string]: RecordLocation },
        }
    },
    getters: {

    },
    actions: {
        addRecentBooks(bookKey: string) {
            return new Promise((resolve, reject) => {
                if (this.recentBooks.includes(bookKey)) {
                    reject('此书已经存在');
                } else {
                    this.recentBooks.push(bookKey)
                    resolve(true)
                }
            })
        },
        deleteRecentBook(bookKey: string): boolean {
            const index = this.recentBooks.indexOf(bookKey);
            if (index > -1) {
                this.recentBooks.splice(index, 1)
                return true
            } else {
                return false;
            }
        },
        addrecordLocation(bookKey: string, locationInfo: RecordLocation) {
            this.recordLocation[bookKey] = locationInfo;
        },

    },
    persist: {
        pick: ['recentBooks', 'recordLocation']
    }
})